import {
  Config
} from "./config.js"
import * as com from "./common.js"
const Modal = new com.Modal()
const Base = new com.Base()

function fetch(url, params) {
  var that = this
  return new Promise((resolve, reject) => {
    wx.request({
      url: Config.baseUrl + url,
      data: params,
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'token': wx.getStorageSync('token') || ''
      },
      success: (res) => {
        console.log('fetch:' + url)
        if (res.data.status == -1) {
          Base.login().then(res => {
            resolve(fetch(url, params))
          })
        } else {
          resolve(res.data)
        }
      },
      fail: (err) => {
        Modal.errModal("网络连接异常")
        reject(err)
        console.log(err)
      }
    })
  })
}

function upload(url, img) {
  wx.showLoading({
    title: '上传中',
    mask: true
  })
  return new Promise((resolve, reject) => {
    wx.uploadFile({
      url: Config.baseUrl + url,
      filePath: img,
      name: 'file',
      header: {
        'token': wx.getStorageSync('sessionId')
      },
      success: (res) => {
        resolve(res.data)
      },
      fail: (err) => {
        if (err.statusCode == 401) {
          this.login()
        }
        Modal.errModal("网络连接异常")
        reject(err)
      },
      complete() {
        wx.hideLoading();
      }
    })
  })
}
export default {
  /**
   * 用户登录
   */
  login(code) {
    return fetch('login', {
      jscode: code
    })
  },
  /**
   * 个人信息存储
   */
  userInfo(params) {
    return fetch('userInfo', params)
  },
  /**
   * 保存图片
   */
  imgsave(img) {
    return upload('imgsave', img)
  },
  /**
   * 图片删除
   * 返回图片url
   */
  imgdel(src) {
    return fetch('imgdel', {
      src: src
    })
  },
  /**
   *获取短信验证码
   *
   */
  getMessage(telephone) {
    return fetch('getMessage', {
      telephone: telephone
    })
  },

  /**
   * 验证短信验证码
   * 返回码:1为正确  0 为失败 失败有msg
   */
  verifymessage(telephone, code) {
    return fetch('checkVerifyMessage', {
      telephone: telephone,
      code: code
    })
  },
  //测试数据
  newGood(params) {
    return fetch('newGood', params)
  },

  // 轮播图数据
  getGwiper(params) {
    return fetch('getGwiper', params)
  },

  // 分类数据
  listCate() {
    return fetch('listCate', {})
  },

  // 商品详情
  getGood(goods_id) {
    return fetch('getGood', {
      good_id: goods_id
    })
  },

  firutGood() {
    return fetch('firutGood', {})
  },

  // 购物车数据
  shopCar(user_id) {
    return fetch('shopCar', {})
  },
  //添加购物车addCar{good_id,num}
  addCar(good_id, num) {
    return fetch('addCar', {
      good_id: good_id,
      num: num
    })
  },

  // 购物车列表购物车列表   listCar{}
  listCar() {
    return fetch('listCar', {})
  },
  //修改数量 upCarNum｛good_id,num｝
  upCarNum(good_id, num) {
    return fetch('upCarNum', {
      good_id: good_id,
      num: num
    })
  },
  //修改选中状态 upCarStatus {good_id,status}
  upCarStatus(good_id, status) {
    return fetch('upCarStatus', {
      good_id: good_id,
      status: status
    })
  },
  //购物车删除商品  delCar {good_id}
  delCar(good_id) {
    return fetch('delCar', {
      good_id: good_id
    })
  },
  //订单确认页(购物车选中状态商品)->点击去付款(保存订单1:生成订单号2.保存商品到order_good 3.生成订单表数据 4.支付接口) shopCar{}
  shopCar() {
    return fetch('shopCar', {})
  },

  // 下单 makeCar
  makeCar(name, tel, address) {
    return fetch('makeCar', {
      username: name,
      tel: tel,
      address: address
    })
  },

  // 已付款
  payed() {
    return fetch('payed', {})
  }

}